home *** CD-ROM | disk | FTP | other *** search
- Path: news.ssnet.com!not-for-mail
- From: helie@ssnet.com (Ray Helie)
- Newsgroups: comp.lang.c++
- Subject: Virtual Members: Difficult Question
- Date: 18 Jan 1996 17:24:06 -0500
- Organization: SSNet, Inc.
- Message-ID: <4dmha6$80c@marlin.ssnet.com>
- NNTP-Posting-Host: marlin.ssnet.com
-
- I have a difficult problem that no one can seem to solve for me. If
- anyone can shed some light, please do:
-
- In the microsoft foundation class library, there is a CObject class. I
- create an inherited class that is a virtual class as follows:
-
- class C_BASE : public CObject
- {
- // ...
- virtual void index () = 0;
- };
-
-
- I don't really use any of the functions out of CObject, but I have to
- inherit it to pass C_BASE to other objects. Anyway, I then define
- a further inherited class that actually defines the virtual function:
-
- class C_ITEM : public C_BASE
- {
- // ...
- void index () { ... };
- };
-
-
- Now here's the question: normally, you can have a base class (with
- a virtual function), and then several derived classes, and using
- a base class pointer, access the virtual function defined in the
- base class, where it will know which derived class' function to
- actually call. Well, I took it possibly a step too far, and I
- need to know if this is ok to do: (and this part is a prelude to the tough
- part):
-
- void main ()
- {
- C_ITEM item;
- func1 ((CObject*)&item);
- }
-
- void func1 (CObject* pItem)
- {
- C_BASE* base;
- base = pItem;
- pItem-> index ();
- // (i) is the above call to index () allowed? or did i lose virtual
- // information about the class C_BASE when I went to the CObject
- // pointer?
- }
-
-
-
- That seems to work for me, but I thought I'd make sure it's something
- that is truly legal that is supposed to work *all* the time.
-
- On to the difficult part:
-
- I'm reading and writing object states to disk. I've have need to do it
- the following way, so what I need to know is why the following way
- doesn't seem to work and how I can get around it:
-
-
- main ()
- {
- C_ITEM item;
- writeObject ((CObject*)&item,sizeof(C_ITEM));
- readObject ((CObject*)&item,sizeof(C_ITEM));
- }
-
-
- void readObject (CObject* pObject,int pSize)
- {
- C_BASE* base = pObject;
- base-> index (); // this call works fine
- Read ((char*)&pObject,pSize);
- // this reads in the character string from beginning of a file
-
- base-> index (); // !!! this call now crashes the program !!!
-
- }
-
-
- void writeObject (CObject* pObject,int pSize)
- {
- Write ((char*)pObject,pSize);
- // this will write a string of characters to the start of a file
- // ...
- }
-
-
- So why does the line in readObject () crash-- it's a virtual function
- call that worked before I read in the data overtop the object. How
- is the knowledge of where the virtual function's address is getting
- corrupted by my writing over the object's memory location? This
- problem seems to require some sort of detailed knowledge of how
- things are truly implemented, so if anyone can shed any insight
- into this problem, I would greatly appreciate it-- no one seems to
- be able to figure it out around here. :) Feel free to email me,
- because I don't know how interesting the answer would be for
- everyone else. Thanks in advance.
-
-
- Ray Helie [helie@ssnet.com]
-